<template>
  <div>
    <nav-bar :title="title" :isShow="false"></nav-bar>
    <van-form @submit="onSubmit">
      <van-field
        v-model="name"
        name="name"
        label="公司名称"
        placeholder="公司名称"
        :rules="[{ required: true, message: '请填写公司名称' }]"
      />
      <van-field
        v-model="contact_person"
        name="contact_person"
        label="联系人"
        placeholder="联系人"
        :rules="[{ required: true, message: '请填写联系人' }]"
      />
      <van-field
        v-model="contact_tel"
        name="contact_tel"
        label="联系电话"
        placeholder="联系电话"
        :rules="[{ required: true, message: '请填写联系电话' }]"
      />
      <van-field
        v-model="agency"
        name="agency"
        label="授权状态"
        placeholder="授权状态"
        readonly
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">保存</van-button>
      </div>
    </van-form>
    <go-home-button></go-home-button>
  </div>
</template>

<script>
import NavBar from '@/components/NavBar'
import GoHomeButton from '@/components/GoHomeButton'
import logo from '@/assets/images/logo.png'
import { agency_company_confirm, agency_company_update }  from '@/api/service' 
import { get_role } from '@/api/role'
import { subscribe_url } from '@/utils/const'

export default {
  components: {
    NavBar,
    GoHomeButton
  },
  props: {
    company_id: String
  },
  data() {
    return {
      title: this.$route.meta.title,
      logo: logo,
      isShow: false,
      name: "",
      contact_person: "",
      contact_tel: "",
      is_agency: 0,
    };
  },
  computed:{
    agency(){
      if(this.is_agency === 1){
        return '已经授权'
      }else{
        return '未授权'
      }
    }
  },
  methods: {
    onClick(){
      this.$router.push("/")
    },
    onSubmit(values) {
        values["id"] = +this.company_id
        agency_company_update(values).then(res=>{
          console.log(res);
          if(res){
            this.$notify({ type: 'success', message: '保存成功' })
          }
          const openid = localStorage.getItem("openid");
          get_role(openid).then(res => {
            if(!res.subscribe){
              window.location.href = subscribe_url
            }
          })
        }).catch(error=>{
            console.log(error);
        })
    },
    AgencyConfirm(company_id){
      agency_company_confirm(company_id).then(res=>{
        console.log(res);
        this.name = res.data.name
        this.contact_person = res.data.contact_person
        this.contact_tel = res.data.contact_tel
        this.is_agency = res.data.is_agency
        if(this.is_agency === 1){
          this.$notify({ type: 'success', message: '授权成功' })
        }else{
          this.$notify({ type: 'warning', message: '授权失败' })
        }
        
      }).catch(error=>{
        console.log(error);
        this.$notify({ type: 'warning', message: '授权失败' })
      })
    }

  },
  created() {
    this.AgencyConfirm(this.company_id)
  },

};
</script>

<style  scoped>

</style>